package Dao.daoImpl;

import Dao.empDao;
import Model.Emp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

import java.sql.*;
import java.util.List;
import java.util.Scanner;

public class EmpDaoImpl implements empDao {
    private QueryRunner queryRunner = new QueryRunner();


    /**
     * 加入
     *
     * @param
     * @param
     */
    @Override
    public void addEmpDao(Emp obj) throws SQLException {
        int result = queryRunner.update(DBUtils.getConnection(),
                "insert into Emp (name,sex,age,job) values(?,?,?,?);",
                obj.getName(), obj.getSex(), obj.getAge(), obj.getJob());
        System.out.println("result = " + result);

    }

    /**
     * 展示
     *
     * @param
     */
    @Override
    public void showEmpDao() throws SQLException {
        System.out.println("编号\t姓名\t性别\t年龄\t工作");

        List<Emp> emp = queryRunner.query(DBUtils.getConnection(),
                "select* from Emp;",
                new BeanListHandler<Emp>(Emp.class));

        for (int i = 0; i < emp.size(); i++) {
            System.out.println(emp.get(i).getEmpId() + "\t" +
                    emp.get(i).getName() + "\t" +
                    emp.get(i).getSex() + "\t" +
                    emp.get(i).getAge() + "\t" +
                    emp.get(i).getJob());
        }


    }

    /**
     * 删除
     *
     * @param
     * @param
     */
    @Override
    public void delEmpDao(int empId) throws SQLException {
        int result = queryRunner.update(DBUtils.getConnection(),
                "delete from Emp where empId=?;", empId);
        System.out.println("result = " + result);
    }

    /**
     * g更改
     *
     * @param
     * @param
     */
    @Override
    public void updateEmpDao(int empId1) throws SQLException {

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你要更新的name是:");
        String name = scanner.next();
        System.out.print("请输入你要更新的sex是:");
        String sex = scanner.next();
        System.out.print("请输入你要更新的age是:");
        String age= scanner.next();
        System.out.print("请输入你要更新的job是:");
        String job = scanner.next();

        int result = queryRunner.update(DBUtils.getConnection(),
                "update Emp set name=?,sex=?,age=?,job=? where empId=?;",
                name, sex, age, job, empId1);
        System.out.println("result = " + result);
    }

//    public void goEmp( Integer empId) throws SQLException {
//        System.out.println("编号\t姓名\t电话\t地址\t租金\t状态(已出租/未出租)");
//        Emp1 emp = queryRunner.query(DBUtils.getConnection(),
//                "select* from Emp1 where empId=?;",
//                new BeanHandler<Emp1>(Emp1.class), empId);
//        //System.out.println("emp = " + emp);
//        System.out.println(emp.getEmpId() + "\t" +
//                emp.getName() + "\t" +
//                emp.getTel() + "\t" +
//                emp.getDress() + "\t" +
//                emp.getMoney() + "\t" +
//                emp.getSta());
//    }
}
